#include "../head/server.h"

extern TCP_info_t TCP_info[MAX_TCPS];

int user_verify(int net_fd, MYSQL *mysql)
{
	train_t train;
	bzero(&train, sizeof(train));

	char opt[2] = {0};
	char username[1024] = {0};
	char password[1024] = {0};
	// 第二次收到客户端消息，判断是 注册or登录
	recv(net_fd, &train.length, sizeof(train.length), 0);
	if (train.length == 0)
	{
		close(net_fd);
		return -1;
	}
	recv(net_fd, train.data, train.length, MSG_WAITALL);
	memcpy(opt, train.data, train.length);

	if (opt[0] == '1') // 注册
	{
		while (1)
		{
			// 接收客户端传输过来的用户名和密码
			bzero(&train, sizeof(train));
			bzero(username, sizeof(username));
			bzero(password, sizeof(password));
			recv(net_fd, &train.length, sizeof(train.length), 0);
			recv(net_fd, train.data, train.length, MSG_WAITALL);
			memcpy(username, train.data, train.length);
			bzero(&train, sizeof(train));
			recv(net_fd, &train.length, sizeof(train.length), 0);
			recv(net_fd, train.data, train.length, MSG_WAITALL);
			memcpy(password, train.data, train.length);

			// 对用户密码使用盐值加密
			char sql[4096] = {0};
			char salt[1024] = {0};
			generate_salt(salt, 19);
			char cryptpassword[1024] = {0};
			strcpy(cryptpassword, crypt(password, salt));

			// 将新用户信息插入user_info表
			sprintf(sql, "insert into user_info (username,salt,cryptpassword,current_path) values ('%s','%s','%s','%s');", username, salt, cryptpassword, "/");
			puts(sql);
			int qret1 = mysql_query(mysql, sql);
			fprintf(stderr, "Insert error %d: %s\n", mysql_errno(mysql), mysql_error(mysql));

			// 在all_user_file表中插入新用户的根目录（/）信息
			bzero(&sql, sizeof(sql));
			sprintf(sql, "insert into all_user_file (filename,username,parent_id,file_path,file_type) values ('%s','%s','%d','%s','%s');", "/", username, -1, "/", "Directory");
			puts(sql);
			int qret2 = mysql_query(mysql, sql);
			fprintf(stderr, "Insert error %d: %s\n", mysql_errno(mysql), mysql_error(mysql));

			if (qret1 != 0 || qret2 != 0)
			{
				bzero(&train, sizeof(train));
				train.length = strlen("register_fail");
				strcpy(train.data, "register_fail");
				send(net_fd, &train, sizeof(train.length) + train.length, MSG_NOSIGNAL);
			}
			else
			{
				bzero(&train, sizeof(train));
				train.length = strlen("register_success");
				strcpy(train.data, "register_success");
				send(net_fd, &train, sizeof(train.length) + train.length, MSG_NOSIGNAL);

				bzero(&train, sizeof(train));
				char token[1024] = {0};

				// 更新TCP_info
				TCP_info[net_fd].net_fd = net_fd;
				time_t current_time = time(NULL);
				TCP_info[net_fd].last_activity = current_time;
				memcpy(TCP_info[net_fd].username, username, sizeof(username));

				jwt_encode(token, username);
				printf("token: %s\n", token);

				train.length = strlen(token);
				memcpy(train.data, token, train.length);
				// 将jwt加密的token发送给用户
				send(net_fd, &train, sizeof(train.length) + train.length, MSG_NOSIGNAL);

				break;
			}
		}
	}

	if (opt[0] == '2') // 登录
	{
		while (1)
		{
			// 从客户端拿到用户名和密码
			bzero(&train, sizeof(train));
			bzero(username, sizeof(username));
			bzero(password, sizeof(password));
			recv(net_fd, &train.length, sizeof(train.length), MSG_WAITALL);
			if (train.length == 0)
			{
				close(net_fd);
				break;
			}
			recv(net_fd, train.data, train.length, MSG_WAITALL);
			memcpy(username, train.data, train.length);

			bzero(&train, sizeof(train));
			recv(net_fd, &train.length, sizeof(train.length), MSG_WAITALL);
			recv(net_fd, train.data, train.length, MSG_WAITALL);
			memcpy(password, train.data, train.length);

			char sql[4096] = {0};
			sprintf(sql, "select * from user_info where username = '%s';", username);
			puts(sql);
			int qret = mysql_query(mysql, sql);
			if (qret != 0)
			{
				fprintf(stderr, "Insert error %d: %s\n", mysql_errno(mysql), mysql_error(mysql));
			}
			MYSQL_RES *res = mysql_store_result(mysql);
			MYSQL_ROW row = mysql_fetch_row(res);
			char *cryptpassword = crypt(password, row[2]);
			if (!strcmp(row[1], username) && !strcmp(row[3], cryptpassword))
			{
				bzero(&sql, sizeof(sql));
				sprintf(sql, "update user_info set current_path = '/' where username = '%s';", username);
				int qret = mysql_query(mysql, sql);
				if (qret != 0)
				{
					fprintf(stderr, "Insert error %d: %s\n", mysql_errno(mysql), mysql_error(mysql));
					return 0;
				}
				puts(sql);

				// 通知用户登录成功
				train.length = strlen("login_success");
				strcpy(train.data, "login_success");
				send(net_fd, &train, sizeof(train.length) + train.length, MSG_NOSIGNAL);
				printf("用户%s登录成功！\n", username);

				// 更新TCP_info
				memcpy(TCP_info[net_fd].username, username, sizeof(username));

				bzero(&train, sizeof(train));
				char token[1024] = {0};
				jwt_encode(token, username);
				printf("token: %s\n", token);

				train.length = strlen(token);
				memcpy(train.data, token, train.length);
				// 将jwt加密的token发送给用户
				send(net_fd, &train, sizeof(train.length) + train.length, MSG_NOSIGNAL);

				mysql_free_result(res);

				break;
			}
			else
			{
				train.length = strlen("login_fail");
				strcpy(train.data, "login_fail");
				send(net_fd, &train, sizeof(train.length) + train.length, MSG_NOSIGNAL);
				mysql_free_result(res);
			}
		}
	}

	return 0;
}